﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GroupManager.Resource.CSDL;

namespace GroupManager.Class
{
    public class LopHoatDong
    {
        // Thêm hoat động
        public static void ThemHoatDong(string TenHoatDong, string NoiDungHoatDong, int MaDoiNhom, int ThangHoatDong, int NamHoatDong, bool HienThi, string Anh)
        {
            // Tạo ra đối tượng kiểu HoatDong
            HoatDong hoatdong = new HoatDong();
            hoatdong.TenHoatDong = TenHoatDong;
            hoatdong.NoiDungHoatDong = NoiDungHoatDong;
            hoatdong.MaHoatDong = MaDoiNhom;
            hoatdong.ThangHoatDong = ThangHoatDong;
            hoatdong.NamHoatDong = NamHoatDong;
            hoatdong.HienThi = HienThi;
            hoatdong.Anh = Anh;
            
            //Thêm vào CSDL
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            db.HoatDongs.InsertOnSubmit(hoatdong);
            db.SubmitChanges();
        }
        
        // Xóa hoạt động
        public static void XoaHoatDong(int MaHoatDong)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();

            //Tìm HoatDong cần xóa
            HoatDong hoatdong = db.HoatDongs.First(p => p.MaHoatDong == MaHoatDong);
            
            //Xóa khỏi CSDL
            db.HoatDongs.DeleteOnSubmit(hoatdong);
            db.SubmitChanges();
        }

        // Sửa hoạt động
        public static void SuaHoatDong(int MaHoatDong, string TenHoatDong, string NoiDungHoatDong, int ThangHoatDong, int NamHoatDong, string Anh)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            HoatDong hoatdong = db.HoatDongs.First(p => p.MaHoatDong == MaHoatDong);
            hoatdong.TenHoatDong = TenHoatDong;
            hoatdong.NoiDungHoatDong = NoiDungHoatDong;
            hoatdong.ThangHoatDong = ThangHoatDong;
            hoatdong.NamHoatDong = NamHoatDong;
            hoatdong.Anh = Anh;
        }

        // Hiện thị lên trang chủ
        public static void HienThiLenTrangChu(int MaHoatDong)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            HoatDong hoatdong = db.HoatDongs.First(p => p.MaHoatDong == MaHoatDong);
            hoatdong.HienThi = true;
            db.SubmitChanges();
        }

        // Lấy thông tin theo mã hoạt động
        public static HoatDong LayThongTinTheoMaHoatDong(int MaHoatDong)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            HoatDong hoatdong = db.HoatDongs.First(p => p.MaHoatDong == MaHoatDong);
            return hoatdong;
        }

        //Lay danh sách hoạt động theo mã đội nhóm
        public static IQueryable<HoatDong> LayDanhSachHoatDongTheoMaDoiNhom(int MaDoiNhom)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            var danhsach = db.HoatDongs.Where(p => p.MaDoiNhom == MaDoiNhom);
	        if(danhsach.Count()==0) return null;
                return danhsach;
        }

        //  lấy danh sách hoạt động mà thành viên chưa tham gia theo mã sinh viên;
        public static IQueryable HoatDongChuaThamGia(string masv)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            var sql1 = db.ThanhVien_HoatDongs.Where(p => p.MaSV == masv);
            var danhsach = from a in db.HoatDongs
                           join b in sql1 on a.MaHoatDong equals b.MaHoatDong into c
                           from d in c.DefaultIfEmpty()
                           where d.MaSV == null
                           select new { a.MaHoatDong, a.TenHoatDong};
            
           return danhsach;
        }

        // Lấy danh sách hoạt động lưu nháp theo mã đội nhóm
        public static IQueryable<HoatDong> LayDanhSachHoatDongLuuNhapTheoMaDoiNhom(int MaDoiNhom)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            var danhsach = db.HoatDongs.Where(p => p.MaDoiNhom == MaDoiNhom && p.HienThi == false);
            if (danhsach.Count() == 0) return null;
            return danhsach;
        }

        // Lấy danh sách hoạt động hiển trị trên trang chủ theo mã đội nhóm
        public static IQueryable<HoatDong> LayDanhSachHoatDongHienThiTrangChuTheoMaDoiNhom(int MaDoiNhom)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            var danhsach = db.HoatDongs.Where(p => p.MaDoiNhom == MaDoiNhom && p.HienThi == true).OrderByDescending(p=> p.MaHoatDong);
            if (danhsach.Count() == 0) return null;
            return danhsach;
        }
        public static int MaAnh()
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            return db.HoatDongs.Count();
        }
    }
}